package com.next

import grails.transaction.Transactional
import org.springframework.security.access.annotation.Secured

import static org.springframework.http.HttpStatus.*

@Secured(['ROLE_ADMINISTRATOR', 'ROLE_COMPANY_ADMINISTRATOR', 'ROLE_COMPANY_USER'])
class ProvinceController
{

    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index(Integer max)
    {
        params.max = Math.min(max ?: 10, 100)
        respond Province.list(params), model: [provinceCount: Province.count()]
    }

    def show(Province province)
    {
        respond province
    }

    def create()
    {
        respond new Province(params)
    }

    @Transactional
    def save(Province province)
    {
        if (province == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (province.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond province.errors, view: 'create'
            return
        }

        province.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'province.label', default: 'Province'), province.id])
                //                redirect province

                redirect(controller: "country", action: "show", params: [id: province?.country?.id])

            }
            '*' { respond province, [status: CREATED] }
        }
    }

    def edit(Province province)
    {
        respond province
    }

    @Transactional
    def update(Province province)
    {
        if (province == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        if (province.hasErrors())
        {
            transactionStatus.setRollbackOnly()
            respond province.errors, view: 'edit'
            return
        }

        province.save flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.updated.message', args: [message(code: 'province.label', default: 'Province'), province.id])
                //                redirect province
                redirect(controller: "country", action: "show", params: [id: province?.country?.id])

            }
            '*' { respond province, [status: OK] }
        }
    }

    @Transactional
    def delete(Province province)
    {

        if (province == null)
        {
            transactionStatus.setRollbackOnly()
            notFound()
            return
        }

        province.delete flush: true

        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.deleted.message', args: [message(code: 'province.label', default: 'Province'), province.id])
                //                redirect action: "index", method: "GET"
                redirect(controller: "country", action: "show", params: [id: province?.country?.id])

            }
            '*' { render status: NO_CONTENT }
        }
    }

    protected void notFound()
    {
        request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.not.found.message', args: [message(code: 'province.label', default: 'Province'), params.id])
                redirect action: "index", method: "GET"
            }
            '*' { render status: NOT_FOUND }
        }
    }

    def searchProvince()
    {
        def name = params["name"]
        String sql = "from Province as u where 1=1"
        if (name)
        {
            sql += " and u.name like '%${name}%'"
        }
        String countSql = "select count(u.id) " + sql
        sql += ' order by createdDate desc'

        def list = Province.findAll(sql, [max: params.max ?: 10, offset: params.offset ?: 0])
        def count = Province.executeQuery(countSql)

        respond list, model: [provinceCount: count[0], params: params], view: 'index'
    }
}
